package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dao.DishDao;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavor;
import com.itheima.dto.DishDto;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao,Dish> implements DishService {


    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 新增菜品，同时保存对应的口味数据，需要操作两张表 ：dish,dish_flavor
     * @param dishDto
     */
    @Transactional         //开启事务
    public void saveWithFlavor(DishDto dishDto) {   //只有一个对象

        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //获取菜品表添加后产生的id
        Long dishId = dishDto.getId(); //菜品id

        //从对象中拿出 口味集合
        List<DishFlavor> flavors = dishDto.getFlavors();

        //将菜品id设置到全部口味中去
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //flavors.forEach( f -> f.setDishId(dishId));

         //口味表添加菜品口味数据
         dishFlavorService.saveBatch(flavors);  //存入的是 集合数据;  方法 saveBatch()

    }

    /**
     * //根据id查询菜品信息dish表和口味信息dish_flavor表
     *            数据回显
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //根据id查询菜品普通字段信息
        Dish dish = this.getById(id);

        //创建DishDto对象
        DishDto dishDto = new DishDto();
        //对象拷贝
        BeanUtils.copyProperties(dish,dishDto);

        //查询口味表信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //查询当前菜品对应的口味信息，从dish_flavor表查询
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());

        //菜品id对应口味dish_id 查询到口味集合
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 根据id修改菜品表以及口味表
     * @param dishDto
     */
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper); //删除原数据

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        //重新设置dish_id值
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        //flavors.forEach(f -> f.setDishId(dishDto.getId()));

        dishFlavorService.saveBatch(flavors); //保存数据
    }

    /**
     * 根据id删除菜品以及口味
     * @param ids
     */
    @Transactional
    public void deleteByIds(List<Long> ids) {

        //构建条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);         // ids数组中 id 对应表中 id
        queryWrapper.eq(Dish::getStatus,1); // 是否为启用状态
        int count = this.count(queryWrapper);    // 以上条件查询
        //判断
        if(count > 0){
            throw new CustomException("菜品起售中，无法删除");
        }

        // 可以删除  先删除口味表
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        // 删除口味表数据
        dishFlavorService.remove(lambdaQueryWrapper);

        // 删除菜品数据
        this.removeByIds(ids);

    }

    /**
     * 批量起售 或 停售
     * @param status
     * @param ids
     */
    @Override
    @Transactional   //开启事务
    public void updateStatus(int status, Long[] ids) {

        //构造条件构造器
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<Dish>();
        //添加条件
        updateWrapper.in(Dish::getId,ids);
        updateWrapper.set(Dish::getStatus,status);

        this.update(updateWrapper);

    }
}
